package com.example.lhh.purchase.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.lhh.purchase.entity.LhhMedicine;
import com.example.lhh.purchase.mapper.LhhMedicineMapper;
import com.example.lhh.purchase.service.ILhhMedicineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.lhh.purchase.vo.LhhMedicineVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author luoHongHu
 * @since 2025-05-01
 */
@Service
public class LhhMedicineServiceImpl extends ServiceImpl<LhhMedicineMapper, LhhMedicine> implements ILhhMedicineService {

    @Resource
    private LhhMedicineMapper lhhMedicineMapper;

    /**
     * 添加
     * @param lhhMedicine
     */
    @Override
    public void addMedicine(LhhMedicine lhhMedicine) {
        lhhMedicineMapper.insert(lhhMedicine);
    }

    /**
     * 通过ID删除药品信息
     * @param id
     */
    @Override
    public void delMedicineById(String id) {
        lhhMedicineMapper.deleteById(id);
    }

    /**
     * 获取药品信息
     * @param lhhMedicineVo
     * @param pageNum 当前页
     * @param pageSize 每页的条数
     * @return
     */
    @Override
    public IPage<LhhMedicine> getMedicineList(LhhMedicineVo lhhMedicineVo, int pageNum, int pageSize) {
        if(StringUtils.isEmpty(pageNum)){
            pageNum=1;
        } else if (StringUtils.isEmpty(pageSize)) {
            pageSize=10;
        }
        QueryWrapper<LhhMedicine> queryWrapper = new QueryWrapper<>();
        String medicineName = lhhMedicineVo.getMedicineName();
        String manufacturer = lhhMedicineVo.getManufacturer();
        if(medicineName != null && medicineName != ""){
            queryWrapper.like("medicine_name",medicineName);
        }else if (manufacturer != null && manufacturer != "") {
            queryWrapper.like("manufacturer",manufacturer);
        }

        IPage<LhhMedicine> lhhMedicineList = null;
        try {
            Page<LhhMedicine> page = new Page<>(pageNum, pageSize);
            lhhMedicineList = lhhMedicineMapper.selectPage(page,queryWrapper);
        }catch (Exception e){
            e.printStackTrace();
        }
        return lhhMedicineList;
    }

}
